<template>
  <div>
    <el-form class="query-table-form" @submit.native.prevent :inline="true" :model="params">
      <el-form-item>
        <el-input
          v-model="params.customer_name"
          placeholder="项目名称查询"
          clearable
          @change="getData()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-input
          v-model="params.corp_name"
          placeholder="负责人查询"
          clearable
          @change="getData()"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <select-address
          v-model="params.address_code"
          placeholder="地址查询"
          auto-top
          clearable
          @change="getData()"
        ></select-address>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" @click="getData()">查询</el-button>
      </el-form-item>
    </el-form>

    <c-table border v-loading="loading" :columns="tableColumns" :data="tableData">
      <template v-slot:address="scope">
        <el-tooltip effect="light" :content="scope.row.address">
          <div class="text-truncate">{{ scope.row.address }}</div>
        </el-tooltip>
      </template>

      <template v-slot:name="scope">
        <el-popover placement="top-start" title="项目经理" trigger="hover">
          <div>
            <div>{{ `A角： ${scope.row.res_a_name || '-'}；` }}</div>
            <div class="mb-2">{{ `电话： ${scope.row.res_a_phone || '-'}` }}</div>
            <div>{{ `B角： ${scope.row.res_b_name || '-'}；` }}</div>
            <div>{{ `电话： ${scope.row.res_b_phone || '-'}` }}</div>
          </div>
          <div slot="reference">
            <div class="status-label badge-info text-left w-100">
              <div>A角:{{ scope.row.res_a_name }}</div>
              <div>B角:{{ scope.row.res_b_name }}</div>
              <div>风控员:{{ scope.row.res_person_risk_point }}</div>
            </div>
          </div>
        </el-popover>
      </template>
    </c-table>

    <div class="query-pagination">
      <div></div>
      <el-pagination
        layout="total, prev, pager, next, jumper"
        :current-page.sync="params.index"
        :page-size="params.size"
        :total="totalCount"
        @current-change="getData"
      ></el-pagination>
    </div>

    <el-dialog
      :visible.sync="reportVisible"
      :close-on-click-modal="false"
      :before-close="beforeClose"
      append-to-body
      top="30px"
      width="80%"
    >
      <el-tabs v-model="activeName">
        <el-tab-pane label="调查报告" name="file">
          <credit-file :query-id="queryId" view-mode></credit-file>
        </el-tab-pane>
        <el-tab-pane label="担保意向函" name="letter">
          <letter-view :queryId="queryId"></letter-view>
        </el-tab-pane>
        <el-tab-pane label="附件">
          <attach-view :queryId="queryId" :visible="reportVisible"></attach-view>
        </el-tab-pane>
      </el-tabs>
    </el-dialog>

    <register-edit-contract-dialog
      :visible.sync="contractVisible"
      @success="handleSuccess"
      :relatedData="contractRelated"
    ></register-edit-contract-dialog>

    <attach-file :customerRelatedId="attachId" :visible.sync="attachVisible"></attach-file>
  </div>
</template>

<script>
import SelectAddress from '@/components/select/SelectAddress'
import CreditFile from '@/credit/components/creditFile/InvestigateReport'
import LetterView from '@/credit/views/county/unionProvince/components/LetterView'
import AttachFile from '@/credit/views/city/program/Attach'
import RegisterEditContractDialog from '@/credit/views/city/self/components/RegisterEditContractDialog'
// import { MANAGE_JOBS } from '@/credit/plugins/constant'
import taskTableMixin from '@/credit/mixin/self/taskTable'
import AttachView from '@/credit/components/AttachView'
import { enumGuatype } from '@/credit/plugins/enumTown'

export default {
  components: {
    SelectAddress,
    CreditFile,
    AttachFile,
    LetterView,
    AttachView,
    RegisterEditContractDialog,
  },
  mixins: [taskTableMixin],
  props: {},
  data() {
    return {
      enumGuatype,
      params: {
        index: 1,
        size: 10,
        customer_name: '',
        corp_name: '',
        address_code: '',
        'gua_type.eq': 2,
        'project_res_status.eq': 2,
        'confuse_status.neq': 3,
        'contract_approval_status.eq': 0,
        subKey: 'project_approval_register.customer_related_id',
      },
      order: {
        N1create_time: 'desc',
        N2bank_accept_date: 'DESC',
      },
      useRegisterTable: true,
      tableColumns: [
        {
          attrs: { label: '主体名称', prop: 'customer_name', minWidth: '120px' },
          process: (v, row) => ({
            attrs: { class: 'project-history', attrs: { 'data-history': row.is_history } },
          }),
        },
        { attrs: { label: '业务类型', prop: 'guaType', align: 'center' } },
        { attrs: { label: '地址', prop: 'address' }, slot: 'address' },
        { attrs: { label: '负责人', prop: 'corp_name', align: 'center' } },
        { attrs: { label: '联系电话', prop: 'mobile_no', align: 'center' } },
        { attrs: { label: '贷款金额(万元)', prop: 'approveFunds', align: 'center' } },
        { attrs: { label: '审批完成时间', prop: 'reviewTime', align: 'center', width: 100 } },
        { attrs: { label: '合作银行', prop: 'bank_name', align: 'center' } },
        { attrs: { label: '项目经理', prop: 'resName', width: 100 }, slot: 'name' },
        {
          attrs: { label: '操作', align: 'center', minWidth: 120 },
          operations: [
            {
              content: '合同审批',
              icon: 'fa fa-gavel',
              event: this.handleEdit,
              ifRender: row =>
                this.userId === row.res_user_id_risk_point && +row.contract_approval_status === 0,
            },
            // {
            //   content: '附件',
            //   icon: 'fa fa-clipboard',
            //   event: this.handleAttach,
            // },
            {
              content: '查看',
              icon: 'fa fa-search',
              event: this.hanleView,
            },
          ],
        },
      ],

      // 担保合同
      contractRelated: {},
      contractVisible: false,

      activeName: 'file',

      // 档案卡
      queryId: '',
      reportVisible: false,
      fileData: {},

      // 附件
      attachId: '',
      attachVisible: false,
    }
  },
  computed: {},
  watch: {},
  created() {
    this.params['service_id.eq'] = this.clientId
    this.getData()
  },
  mounted() {},
  methods: {
    dataAdapter(item) {
      item.guaType = enumGuatype[item.gua_type] || '-'
      item.reviewTime = this.$utils.dateFormat(
        item.res_date_corp || item.res_date,
        'yyyy-MM-dd hh:mm'
      )
      item.resName = `A角: ${item.res_a_name} B角: ${item.res_b_name} 风控员：${item.res_person_risk_point}`
      const projectApproval = item.project_approval_register || []
      const approve = projectApproval.find(item => +item.approval_register_type === 2) || {}
      item.approveFunds = approve.res_funds || ''

      return item
    },
    handleAttach(row) {
      this.attachId = row.customer_related_id
      this.attachVisible = true
    },
    handleEdit(row = {}) {
      this.contractVisible = true
      this.contractRelated = { ...row }
    },
    hanleView(row) {
      this.showFile(row)
    },
    showFile(row = {}) {
      this.fileData = row
      this.queryId = this.fileData.customer_related_id
      this.reportVisible = true
    },
    beforeClose() {
      this.reportVisible = false
      setTimeout(() => (this.activeName = 'file'), 200)
    },
    handleSuccess() {
      this.$store.dispatch('getTaskCount')
      this.getData()
    },
  },
}
</script>
<style lang="scss" scoped>
.status-label {
  display: inline-block;
  padding: 0.25em 0.4em;
  font-size: 75%;
  font-weight: 700;
  line-height: 1;
  text-align: center;
  vertical-align: baseline;
  border-radius: 0.25rem;
  transition: color 0.15s;
}
</style>
